import java.util.ArrayList;

public class ActiveOrderRepository {

	private static ActiveOrderRepository repository;
	private static ArrayList<ActiveOrder> activeOrders = new ArrayList<ActiveOrder>();

	private ActiveOrderRepository() {
	}

	public static ActiveOrderRepository getOrderRepository() {
		if (repository == null) {
			repository = new ActiveOrderRepository();
		}
		return repository;
	}

	public void addActiveOrder(ActiveOrder activeOrder) {
		activeOrders.add(activeOrder);
	}

	public ArrayList<ActiveOrder> getActiveOrders() {
		return activeOrders;
	}

	public ActiveOrder getActiveOrder(int activeOrderId) {
		for (int i = 0; i < activeOrders.size(); i++) {
			ActiveOrder activeOrder = activeOrders.get(i);
			if (activeOrder.getActiveOrderId() == activeOrderId) {
				return activeOrders.get(i);
			}
		}
		return null;
	}

	public boolean removeActiveOrder(int activeOrderId) {
		for (int i = 0; i < activeOrders.size(); i++) {
			ActiveOrder activeOrder = activeOrders.get(i);
			if (activeOrder.getActiveOrderId() == activeOrderId) {
				activeOrders.remove(i);
				return true;
			}
		}
		return false;
	}

	public void deleteExpiredOrders() {
		for (int i = 0; i < activeOrders.size(); i++) {
			ActiveOrder activeOrder = activeOrders.get(i);
			if (Timer.getDate().before(activeOrder.getOrder().getEndTime())) {
				activeOrders.remove(i);
			}
		}
	}

	public void deleteMutualFundOrders() {
		for (int i = 0; i < activeOrders.size(); i++) {
			ActiveOrder activeOrder = activeOrders.get(i);
			if (SecurityType.MUTUAL_FUNDS.equals(activeOrder.order
					.getSecurityType())) {
				activeOrders.remove(i);
			}
		}
	}

}
